package com.lian.chatroom.server.handler;

import com.lian.chatroom.message.GroupChatRequestMessage;
import com.lian.chatroom.message.GroupChatResponseMessage;
import com.lian.chatroom.server.session.GroupSessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.List;

/**
 * 群聊
 */
@ChannelHandler.Sharable
public class GroupChatRequestMessageHandler extends SimpleChannelInboundHandler<GroupChatRequestMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupChatRequestMessage msg) throws Exception {
        String content = msg.getContent();
        String groupName = msg.getGroupName();
        String from = msg.getFrom();
        //获取全部群成员的通道
        List<Channel> membersChannels = GroupSessionFactory.getGroupSession().getMembersChannel(groupName);
        for (Channel membersChannel : membersChannels) {
            //将消息是谁发的，发的内容，都写到收信人的通道里
            membersChannel.writeAndFlush(new GroupChatResponseMessage(from,content));
        }
    }
}
